package com.example.jones.function;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jones.test.MainActivity;
import com.example.jones.test.R;
import com.example.util.HttpUtil;
import com.example.util.HttpUtilWithParameter;
import com.tencent.connect.dataprovider.ErrorCode;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.UiError;

public class LoginView {

    private Context mcontext = null;
    private View mView = null;
    private TextView username1, password1;
    private Button login, register_start, third_account;
    private RegisterView registerView = null;
    private LinearLayout loginlayout = null;

    public LoginView(Context c) {
        this.mcontext = c;
    }

    public View getView() {
        System.out.println("####################进入LoginActivity");
        mView = View.inflate(mcontext, R.layout.login, null);

        username1 = (TextView) mView.findViewById(R.id.username1);
        password1 = (TextView) mView.findViewById(R.id.password1);
        login = (Button) mView.findViewById(R.id.login);
        register_start = (Button) mView.findViewById(R.id.register_start);
        third_account = (Button) mView.findViewById(R.id.qq_account);
        loginlayout = (LinearLayout) mView.findViewById(R.id.login_layout);

        login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String url = HttpUtil.Base_url + "/LoginService";
                try {
                    if (username1.getText().toString().equals("")
                            || password1.getText().toString().equals("")) {
                        Toast.makeText(mcontext, "请填写用户ID和密码！", 1000).show();
                        return;
                    } else {
                        if (HttpUtilWithParameter.ValidUser(url, username1
                                .getText().toString(), password1.getText()
                                .toString())) {
                            Toast.makeText(mcontext, "登录成功！", 1000).show();
                        } else {
                            Toast.makeText(mcontext, "用户ID或密码错误！", 1000).show();
                            return;
                        }
                    }
                    ;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        register_start.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                loginlayout.removeAllViews();
                if (registerView == null) {
                    registerView = new RegisterView(mcontext);
                }
                loginlayout.addView(registerView.getView());
            }
        });

//        third_account.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                if ((null != ((MainActivity) mcontext).mTencent)
//                        && (!((MainActivity) mcontext).mTencent
//                                .isSessionValid())) {
//                    IUiListener listener = new BaseUiListener() {
//                        @Override
//                        protected void doComplete(JSONObject values) {
//                            Toast.makeText(mcontext, "登录成功，即将进入主页",
//                                    Toast.LENGTH_SHORT).show();
//                            Toast.makeText(mcontext, values.toString(), Toast.LENGTH_SHORT).show();
//                            Intent it = new Intent(mcontext, MainActivity.class);
//                            mcontext.startActivity(it);
//                        }
//                    };
//                    // mQQAuth.login(this, "all", listener);
//                    // mTencent.loginWithOEM(this, "all",
//                    // listener,"10000144","10000144","xxxx");
//                    ((MainActivity) mcontext).mTencent.login(
//                            (MainActivity) mcontext, "get_user_info,add_share",
//                            listener);
//
//                } else {
//                    Intent it = new Intent(mcontext, MainActivity.class);
//                    mcontext.startActivity(it);
//                }
//            }
//        });
        return mView;
    }

    public class BaseUiListener implements IUiListener {

        @Override
        public void onComplete(Object response) {
            doComplete((JSONObject) response);
        }

        protected void doComplete(JSONObject values) {
            Toast.makeText(mcontext, values.toString(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(UiError e) {
            Toast.makeText(mcontext, e.toString(), Toast.LENGTH_SHORT).show(); 
        }

        @Override
        public void onCancel() {
        }
    }
}
